R code

Necessary packages

library(dplyr)
library(ggplot2)
library(leaflet)
library(geojsonio)
library(sp)

Loading data

Table of CO2 emission: https://data.worldbank.org/indicator/EN.ATM.CO2E.PC

World countries map: https://datahub.io/core/geo-countries#data

c02 <- read.csv("C:/Users/Dell/OneDrive/Dokumenty/R/WdED/SkalskiJan/hw4/c02_emissions.csv")

world <- geojsonio::geojson_read("C:/Users/Dell/Downloads/countries.geojson", what = "sp")

Adjusting data to suitable format

c02 <- c02 %>%
  select(Country.Name, X2019) %>% 
  transmute(country = Country.Name, emission = round(X2019, 3)) %>% 
  na.omit()

world <- sp::merge(world, c02,
               by.x = "ADMIN", by.y = "country")

Leaflet map

bins <- c(0, 1, 2, 3, 5, 10, 15, 20, 35)
pal <- colorBin("YlOrRd", domain = world$emission, bins = bins, na.color = NA)

labels <- sprintf(
  "<strong>%s</strong><br/>%g tones",
  world$ADMIN, world$emission
) %>% lapply(htmltools::HTML)

leaflet(world) %>% 
  addTiles(options = (providerTileOptions(noWrap = TRUE,
                                          minZoom = 1.5,
                                          maxZoom = 12))) %>% 
  addPolygons(fillColor = ~pal(emission),
              weight = 2,
              opacity = 1,
              color = "white",
              dashArray = "3",
              fillOpacity = 0.7,
              highlightOptions = highlightOptions(
                weight = 5,
                color = "#666",
                dashArray = "",
                fillOpacity = 0.7,
                bringToFront = TRUE),
              label = labels,
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px"),
                textsize = "15px",
                direction = "auto")) %>% 
  addLegend(pal = pal, values = ~emission, opacity = 0.7, title = "Annual emission of CO2 per capita in tones",
            position = "bottomright") %>% 
  setView(10, 10, 1.5) %>% 
  setMaxBounds(-180,-90,180,90)

Data for map is from 2019